#!/usr/bin/env perl

=head1 NAME

ResultWriter

=head1 SYNOPSIS

use ResultWriter;

my $resultWriter = Resultwriter->new(filepath=>"./", separator=>"\t");
$resultWriter->write(\@subcomplexes, "1e79", $r, $k, $a, $c)

=head1 DESCRIPTION

Writes identified subcomplexes to file. For each query like '10gs' a summary file '10gs_summary' and a file with
the detailed mappings '10gs_mapping' is created. Each subcomplex is indexed such that 'summary' and 'mapping' files
can be combined in a relational way. Specifically, 'summary' stores the number of domains, RMSD, total deviation, score
and all program parameters (e.g, maxRMSD). The mapping of each single domain is stored in the 'mapping' file, together 
with its local deviation.  

=head1 REQUIRES

sbglib, PDL

=head1 AUTHOR

Philipp Harth <phil.harth@gmail.com>

=cut

use PDL;

use Scorer;
use SubcomplexFinder;
use SubcomplexMerger;

use SBG::DomainIO::cofm;

package ResultWriter;
use Moose;

has 'indexNumber' => (is=>'rw', default=>1);
has 'legendPrinted' => (is=>'rw', default=>0);
has 'filepath' => (is=>'rw', default=>"./");
has 'query' => (is=>'rw', default=>"unset");
has 'separator' => (is=>'rw', default=>"\t");


=head2 write
 Function: writes identified subcomplexes to file
 Example : 
 Returns : 
 Args    : list of subcomplexes, query-pdbID, r, k, a, c
 
 The first argument is a reference to the list of subcomplexes to be printed. The second argument is
 the query's PDB-ID that is used to name the result files. Arguments 3 to 6 are the program parameters
 'r' (maxRMSD), 'k' (best k results), 'a' (approximation treshold), and 'c' (best c candidates) that 
 were used.
 
=cut
sub write{
	my ($self, $subcomplexes, $query, $r, $k, $a, $c)=@_;
	
	#reset and create new files if query is new 
	if($query ne $self->query){
		$self->reset;
		$self->query($query);
	}
	#print legend if required
	if(! $self->legendPrinted){
		$self->printLegend;
	}
	
	#open files
	my $path = join '','>>',$self->filepath, $self->query, "_summary"; 
	open SUMMARY, $path or die $!;
	$path = join '','>>',$self->filepath, $self->query, "_mapping"; 
	open MAPPING, $path or die $!;
	
	#for all subcomplexes
	for my $s (@{$subcomplexes}){
		#write summary
		my $data = join $self->separator, $self->indexNumber, $s->complexA, $s->complexB, $s->numberOfDomains,
						sprintf("%.2f",$s->rmsd), sprintf("%.2f",$s->deviation), sprintf("%.0f",$s->score), sprintf("%.2f",$r), $k, $a, $c;
		print SUMMARY ($data, ,$/);
		#write mapping
		my $nDomains = scalar(@{$s->domainsA});
		for(my $i=0; $i<$nDomains; $i++){
			my $data = join $self->separator, $self->indexNumber, $s->domainsA->[$i]->id, $s->domainsB->[$i]->id, 
							sprintf("%.2f",PDL::sclr($s->localDeviations->slice($i)));
			print MAPPING ($data,$/);
		}
		$self->indexNumber($self->indexNumber+1);
	}
	
	#close files
	close SUMMARY;
	close MAPPING;
}



=head2 printLegend

 Function: prints legend at top of summary and mapping files
 Example : 
 Returns : 
 Args    : 
 
=cut
sub printLegend{
	my ($self)=@_;
	
	#write legend in summary file
	my $path = join '','>' ,$self->filepath, $self->query, "_summary";
	open SUMMARY, $path or die $!;
	my $legend = join $self->separator, "index","complex1(query)","complex2","nDomains","RMSD","deviation", 
								"score","maxRMSD(r)","topK(k)","approximationThreshold(a)","topCandidates(c)"; 
	print SUMMARY ($legend,$/);
	close SUMMARY;
	
	#write legend in mapping file
	$path = join '', '>', $self->filepath, $self->query, "_mapping";
	open MAPPING, $path or die $!;
	$legend = join $self->separator,"index","domain1(query)","domain2","deviation"; 
	print MAPPING ($legend,$/);
	close MAPPING;
	
	#set legend printed flag
	$self->legendPrinted(1);
}

=head2 reset

 Function: resets ResultWriter 
 Example : 
 Returns : 
 Args    : 
 
 Function is called automatically, when the results of a new query are printed.
 
=cut
sub reset{
	my ($self)=@_;
	$self->indexNumber(1);
	$self->legendPrinted(0);
}

no Moose;
1;